Java Generics in a list, how to process List

Up vote 1 down vote favorite share g+ share fb share tw.

I have a class, MyObject and a list: List>. Contents of the list (example): MyObject(); MyObject(); Then the MyObject class contains a function: public T getValue() { return this. SomeAttribute; } How can I iterate over my List and call the getValue method?

Java generics wildcard link|improve this question edited Mar 22 '11 at 9:07irreputable12.6k2821 asked Mar 22 '11 at 7:26Jordi30318.

For (myObject obj : aList) { obj.getValue(); }.

List> t = new ArrayList>(); Iterator> iter = t.iterator(); while (iter.hasNext()) { Myclass next = iter.next(); Object value = next.getValue(); System.out. Println(value); }.

The point is, the result of the item should be type specific. It is part of a parsing framework. – Jordi Mar 22 '11 at 7:40 2 AS long as you use?

, you will not be able to get type specific results. From what I understand from your problem, the best you can do is to use some form of MyClass and then get MyType as type during iteration. – Suraj Chandran Mar 22 '11 at 7:49 1 @Jordi, Suraj is right.

So long as you have List> you're saying "I have a list that contains MyObjects of some type, but I don't know what that type is. " In that case there's no way you can use the specific type, because you've just implied that you don't know it. If you do know it (e.g. It's Double), then declare the list as List>.

If the type changes for different instance of the class, but is identical within a single instance, then generic parameterisation should help you. – Andrzej Doyle Mar 22 '11 at 8:29.

Try this for (MyObject obj : yourList){ T value = obj.getValue(); }.

1 I have some issues with this - firstly you're using the raw type MyObject, which is almost never a good thing (arguably should have been disallowed in the 1.5+ compilers) and you should provide some generic parameter. Secondly, where is the parameter T defined? The list is of type List>, so there's no assumption that a suitable T would be in scope.

Finally, the getValue() call will not return a T but will either return Object or? (depending on whether you address the second point), so this will either fail to compile or (correctly) raise an unchecked cast. – Andrzej Doyle Mar 22 '11 at 8:26.

The point is, the result of the item should be type specific. It is part of a parsing framework. The results can't be type specific, because your list contains a mixture of parametized MyObject (Note that I use upper case notation from now on...).

The result will be of type Object and you'll have to use instanceof and cast. Example code: public class MyObject { private T value; public T getValue() {return value;} // ... } Now we make a list snippet: List> list = new ArrayList>(){ new MyObject(), new MyObject()}; for (MyObject item:list) { Object itemValue = list.getValue(); } From this example we see that we can't cast to any other type but Object - because the collection contains MyObject instances whose getValue() methods return Integer or String.

There's no problem to call the method, the question is what you need to do with the return value. For (MyObject obj : aList) call( obj ); static call(MyObject obj) { T val = obj.getValue(); } We can feed a MyObject to call() method as a MyObject arg, due to wildcard capture.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.